package com.industrial.premu.controller;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.KvDomain;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.service.IBigPingService;
import com.industrial.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 亩产大屏
 *
 * @author SangGuangCan
 * @date 2024-03-25
 */
@RestController
@RequestMapping("/bigping")
public class BigPingController {

    @Autowired
    private IBigPingService bigPingService;

    /**
     * 查询亩产中的所有年份
     *
     * @return
     */
    @GetMapping("/queryAllDataYears")
    public AjaxResult queryAllDataYears() {
        List<String> yearsList = bigPingService.queryAllDataYears();
        return AjaxResult.success(yearsList);
    }

    /**
     * 查询企业评价信息
     *
     * @return
     */
//    @RequiresPermissions("premu:bigping:companyinfo")
    @GetMapping("/queryCompnayEvaluateInfo")
    public AjaxResult queryCompanyEvaluateInfo(PreMuBenefitEvaluation params) {
        
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        //用户的角色
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
//        if (sysUser.isAdmin()) {
//
//        } else {
//            //通过角色来限制查询到的数据
//            if (ArrayUtil.contains(roles.toArray(), "x_01_011")) {
//                params.setProvince(sysUser.getProvince());
//                params.setCity(sysUser.getCity());
//                params.setCounty(sysUser.getDistrict());
//            } else if (ArrayUtil.contains(roles.toArray(), "s_01_011")) {
//                params.setProvince(sysUser.getProvince());
//                params.setCity(sysUser.getCity());
//            } else if (ArrayUtil.contains(roles.toArray(), "p_01_011")) {
//                params.setProvince(sysUser.getProvince());
//            }
//        }

        if(sysUser.isAdmin()){
            roles.add("p_01_011");
        }

        //通过角色来限制查询到的数据
        if (ArrayUtil.contains(roles.toArray(), "x_01_011")) {
            params.setProvince(sysUser.getProvince());
            params.setCity(sysUser.getCity());
            params.setCounty(sysUser.getDistrict());
        } else if (ArrayUtil.contains(roles.toArray(), "s_01_011")) {
            params.setProvince(sysUser.getProvince());
            params.setCity(sysUser.getCity());
        } else if (ArrayUtil.contains(roles.toArray(), "p_01_011")) {
            params.setProvince(sysUser.getProvince());
        }

        //设置要查询的年份，今年减一年  年份不需要是前端传来的
//        LocalDate localDate = LocalDate.now();
//        params.setYears(String.valueOf(localDate.getYear() - 1));

        Map<String, Object> result = bigPingService.queryCompanyEvaluateInfo(params);
        return AjaxResult.success(result);
    }

    /**
     * 查大屏中工业总产值 销售收入等信息
     *
     * @return
     */
    @GetMapping("/queryPreMuReactionInformation")
    public AjaxResult queryPreMuReactionInformation(PreMuBenefitEvaluation params) {
//        LocalDate localDate = LocalDate.now();
//        String years = String.valueOf(localDate.getYear() - 1);
        Map<String, Object> resultMap = bigPingService.queryPreMuReactionInformation(params);
        return AjaxResult.success(resultMap);
    }

    /**
     * 查询地市等级分布
     *
     * @return years 年份  mjLevel 等级
     */
    @GetMapping("/queryCityLevel")
    public AjaxResult queryCityLevel(@RequestParam Map<String, String> params) {
        List<Map<String, String>> resultMap = bigPingService.queryCityLevel(params);
        return AjaxResult.success(resultMap);
    }

    /**
     * 查询地图上的数据
     *
     * @return years 年份
     */
    @GetMapping("/queryMapData")
    public AjaxResult queryMapData(PreMuBenefitEvaluation params) {
//        LocalDate localDate = LocalDate.now();
//        String years = String.valueOf(localDate.getYear() - 1);
        List<Map> map = bigPingService.queryMapData(params);
        return AjaxResult.success(map);
    }

    /**
     * 查询各行业分布统计TOP5
     *
     * @param params indexName 指标名称
     * @return
     */
    @GetMapping("/queryIndustryTop")
    public AjaxResult queryIndustryTop5(@RequestParam Map<String, String> params) {
        Map<String, Object> resultMap = bigPingService.queryIndustryTop5(params);
        return AjaxResult.success(resultMap);
    }

    /**
     * 查询地市企业指标分析
     *
     * @param params indexName 指标名称
     * @return
     */
    @GetMapping("/queryCityIndexTop")
    public AjaxResult queryCityIndexTop5(@RequestParam Map<String, String> params) {
        Map<String, Object> resultMap = bigPingService.queryCityIndexTop5(params);
        return AjaxResult.success(resultMap);
    }
}
